package cn.dancepeak.artcle.service.impl;

import cn.dancepeak.artcle.mapper.ArticleMapper;
import cn.dancepeak.artcle.service.IArticleServiceImpl;
import cn.dancepeak.article.domain.Article;
import cn.dancepeak.article.domain.ArticleType;
import cn.dancepeak.article.query.ArticleQuery;
import cn.dancepeak.vo.ArticleTypeDateNumVo;
import cn.dancepeak.vo.ArticleTypeNameNumVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImplImpl implements IArticleServiceImpl {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void add(Article article) {
        //密码新增以后要加密
        articleMapper.insert(article);

        //添加到mongodb
        mongoTemplate.save(article);
    }

    @Override
    public void update(Article article) {
        //用户名不能修改   禁用  在前端写
        articleMapper.updateById(article);
        //修改mongodb的内容 用save
        mongoTemplate.save(article);
    }

    @Override
    public void delete(Long id) {
        articleMapper.deleteById(id);
        //删除mongodb的内容
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Article.class);
    }

    @Override
    public void deleteByList(List<Long> ids) {
        articleMapper.deleteByList(ids);
        //批量删除mongodb的内容
        for (Long id : ids) {
            Query query = new Query(Criteria.where("id").is(id));
            mongoTemplate.remove(query,Article.class);
        }
    }

    @Override
    public Article getById(Long id) {
        return articleMapper.selectById(id);
    }

    @Override
    public PageInfo<Article> page(ArticleQuery articleQuery) {
        // 1.下面这句代码表示开启分页，会将紧挨着这句代码的下一次数据库执行进行分页，切记一定要放在要分页的SQL语句前面执行
        // 2.执行SQL语句带条件查询，SQL语句不需要进行分页
        PageHelper.startPage(articleQuery.getCurrentPage(), articleQuery.getPageSize());
        List<Article> article = articleMapper.list(articleQuery);
        // 3.将查询结果转换为PageInfo对象，PageInfo对象中有前端分页所需要的各种参数
        PageInfo<Article> pageInfo = new PageInfo<>(article);
        // 4.响应总条数和分页信息
        return pageInfo;
    }

    @Override
    public List<ArticleTypeNameNumVo> getArticleTypeNameNum() {
        return articleMapper.getArticleTypeNameNum();
    }

    @Override
    public List<ArticleTypeDateNumVo> getArticleTypeDateNum() {
        return articleMapper.getArticleTypeDateNum();
    }

    @Override
    public List<Article> getRelevant(Long id, String articleTag) {
        return articleMapper.getRelevant(id,articleTag);
    }

    @Override
    public List<Article> getArticleByTypeName(String articleName) {
        return articleMapper.getArticleByTypeName(articleName);
    }

    @Override
    public List<Article> getArticleNameAndArticleStarNum() {
        return articleMapper.getArticleNameAndArticleStarNum();
    }

    @Override
    public Map<String, Integer> getArticleTagAndArticleReadSum() {
        List<Article> list = articleMapper.getArticleTagAndArticleReadSum();
        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Article::getArticleTypeName,Article::getNum));
        return map;
    }

    @Override
    public void updateReadAddOne(Long id) {
        articleMapper.updateReadAddOne(id);
    }
}
